﻿using System;
using System.Collections.Generic;

namespace XHFrame
{
    public class PropertyItem
    {
        public int ID { get; set; }
        private object content;
        private object rawContent;
        private bool canRandom = false;
        private int curRandomIntValue;
        private float curRandomFloatValue;
        private Type PropertyType;

        //  owner 持有者

        public IDynamicProperty Owner = null;

        public object Content
        {
            get
            {
                return GetContent();
            }

            set
            {
                if (value != GetContent())
                {
                    object oldContent = GetContent();
                    SetContent(value);
                    if (Owner != null)
                    {
                        Owner.DoChangeProperty(ID, oldContent, value);
                    }
                }
                Content = value;
            }
        }

        public void SetValueWithoutEvent(object Content)
        {
            if (Content != GetContent())
            {
                object oldContent = GetContent();
                SetContent(Content);
            }
        }

        /// <summary>
        /// 获取原始属性
        /// </summary>
        public object RawContent
        {
            get
            {
                return rawContent;
            }
        }

        /// <summary>
        /// 属性
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="content"></param>
        public PropertyItem(int ID, object content)
        {
            PropertyType = content.GetType();
            if (PropertyType == typeof(System.Int32) || PropertyType == typeof(System.Single))
            {
                canRandom = true;
            }
            this.ID = ID;
            SetContent(content);
            this.Content = content;

        }

        private void SetContent(object content)
        {
            if (canRandom)
            {
                if (PropertyType == typeof(System.Int32))
                {
                    curRandomIntValue = UnityEngine.Random.Range(1, 1000);
                    this.content = (int)content + curRandomIntValue;
                }
                else if (PropertyType == typeof(System.Single))
                {
                    curRandomFloatValue = UnityEngine.Random.Range(1, 1000);
                    this.content = (float)content + curRandomFloatValue;
                }
            }
            else
            {
                this.content = content;
            }
        }

        private object GetContent()
        {
            if (canRandom)
            {
                if (PropertyType == typeof(System.Int32))
                {
                    return (int)content - curRandomIntValue;
                }
                else if (PropertyType == typeof(System.Single))
                {
                    return (float)content - curRandomFloatValue;
                }
            }
            return null;
        }
    }
}
